<?php
class model_model extends MY_Model {

    function __construct(){
        parent::__construct();
    }

    function get_opt(){
        $opt_model = array();
        $list_model = $this->cm->get_all($this->tbl->model);
        foreach ($list_model as $k => $v) {
            $opt_model[$v->id] = $v->title;
        }
        return $opt_model;
    }

    function new_tbl($tbl_name){
        $sql = "
            CREATE TABLE `".CUSTOM_TBL_PREFIX.$tbl_name."` (
                `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
                `disable` ENUM( 'true', 'false' ) NOT NULL DEFAULT 'false',
                `utime` INT NOT NULL ,
                `atime` INT NOT NULL ,
                INDEX ( `disable` ) 
            ) ENGINE = MYISAM 
        ";
            return $this->db->query($sql);        
    }

    function del_tbl($tbl_name){
        $sql = "
            DROP TABLE IF EXISTS `".CUSTOM_TBL_PREFIX.$tbl_name."`;
        ";
        return $this->db->query($sql);
    }

    function tbl_add_col($tbl_name,$col_name,&$type,&$value='',&$default='',$is_allow_null='N'){
        $col_type = 'CHAR( 200 )';
        if($type == 'textarea'){
            $col_type = 'TEXT';
        }elseif($type == 'html'){
            $col_type = 'LONGTEXT';
        }elseif($type == 'int'){
            $value = intval($value);
            if($value <= 0 || $value>255){
                $value = 11;
            }
            if(!empty($value)){
                $col_type = 'INT('.$value.')';
            }else{
                $col_type = 'INT(10)';
            }
        }elseif($type == 'money'){
            $col_type = 'decimal(10,3)';

        }elseif($type == 'float'){
            $col_type = 'FLOAT';
        }elseif(in_array($type, array('select','radio','checkbox'))){
            if(empty($value)) return false;
            $arr_v = explode(';', $value);
            $str_v = '';
            foreach ($arr_v as $k => $v) {
                if(empty($v)) continue;
                $arr_sv = explode(':', $v);
                if(empty($arr_sv[0])) continue;
                $str_v .= "'".$arr_sv[0]."',";
            }
            $col_type = "ENUM(".trim($str_v,',').")";
        }elseif($type == 'image'){
            $col_type = 'int(10)';
        }elseif($type == 'images'){
            $col_type = 'varchar(200)';
        }

        $col_default = '';
        if(!empty($default)){
            
            $col_default = 'DEFAULT \''.$default.'\'';

            if(in_array($type, array('html','textarea'))){
                $col_default = '';                
            }

            if(in_array($type, array('int'))){
                $col_default = 'DEFAULT \''.intval($default).'\'';   
            }

            if(in_array($type, array('money','float'))){
                $col_default = 'DEFAULT \''.floatval($default).'\'';   
            }

            if(in_array($type, array('select','radio','checkbox'))){

                $arr_v = explode(';', $value);
                $arr_index = array();
                foreach ($arr_v as $k => $v) {
                    if(empty($v)) continue;
                    $arr_sv = explode(':', $v);
                    if(empty($arr_sv[0])) continue;
                    $arr_index[] = $arr_sv[0];
                }
                if(!in_array($default, $arr_index)){
                    $col_default = '';   
                }
            }

        }

        $col_is_null = '';
        if(!empty($is_allow_null) && $is_allow_null == 'N'){
            $col_is_null = 'NOT NULL';
        }

        $sql = "
            ALTER TABLE `".CUSTOM_TBL_PREFIX.$tbl_name."` ADD `".$col_name."` ".$col_type." ".$col_is_null." ".$col_default."
        ";

        return $this->db->query($sql);
    }

    function tbl_edit_col($tbl_name,$o_col_name,$c_col_name,$type,$value,$default='',$is_allow_null='N'){
        $col_type = 'CHAR( 200 )';
        if($type == 'textarea'){
            $col_type = 'TEXT';
        }elseif($type == 'html'){
            $col_type = 'LONGTEXT';
        }elseif($type == 'int'){
            $value = intval($value);
            if($value <= 0 || $value>255){
                $value = 11;
            }
            if(!empty($value)){
                $col_type = 'INT('.$value.')';
            }else{
                $col_type = 'INT(10)';
            }
        }elseif($type == 'money'){
            $col_type = 'decimal(10,3)';

        }elseif($type == 'float'){
            $col_type = 'FLOAT';
        }elseif(in_array($type, array('select','radio','checkbox'))){
            if(empty($value)) return false;
            $arr_v = explode(';', $value);
            $str_v = '';
            foreach ($arr_v as $k => $v) {
                if(empty($v)) continue;
                $arr_sv = explode(':', $v);
                if(empty($arr_sv[0])) continue;
                $str_v .= "'".$arr_sv[0]."',";
            }
            $col_type = "ENUM(".trim($str_v,',').")";
        }elseif($type == 'image'){
            $col_type = 'int(10)';
        }elseif($type == 'images'){
            $col_type = 'varchar(200)';
        }

        $col_default = '';
        if(!empty($default)){
            
            $col_default = 'DEFAULT \''.$default.'\'';

            if(in_array($type, array('html','textarea'))){
                $col_default = '';                
            }

            if(in_array($type, array('int'))){
                $col_default = 'DEFAULT \''.intval($default).'\'';   
            }

            if(in_array($type, array('money','float'))){
                $col_default = 'DEFAULT \''.floatval($default).'\'';   
            }

            if(in_array($type, array('select','radio','checkbox'))){

                $arr_v = explode(';', $value);
                $arr_index = array();
                foreach ($arr_v as $k => $v) {
                    if(empty($v)) continue;
                    $arr_sv = explode(':', $v);
                    if(empty($arr_sv[0])) continue;
                    $arr_index[] = $arr_sv[0];
                }
                if(!in_array($default, $arr_index)){
                    $col_default = '';   
                }
            }

            if(in_array($type, array('image','images'))){
                $col_default = '';   
            }
        }

        $col_is_null = '';
        if(!empty($is_allow_null) && $is_allow_null == 'N'){
            $col_is_null = 'NOT NULL';
        }

        $sql = "
            ALTER TABLE `".CUSTOM_TBL_PREFIX.$tbl_name."` CHANGE `".$o_col_name."` `".$c_col_name."` ".$col_type." ".$col_is_null." ".$col_default."
        ";

        return $this->db->query($sql);
    }

    function tbl_del_col($tbl_name,$col_name){

        $sql = "DESC `".CUSTOM_TBL_PREFIX.$tbl_name."`";
        $query = $this->db->query($sql);
        $flag = false;
        foreach ($query->result() as $r){
            if($r->Field == $col_name) {
                $flag = true;
                break;
            }
        }        

        if($flag){
            $sql = "
                ALTER TABLE  `".CUSTOM_TBL_PREFIX.$tbl_name."` DROP `".$col_name."`;
            ";

            return $this->db->query($sql);
        }

        return true;

    }

}


